package com.qdsg.modulesihealsdk.ecg12;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;

import java.io.FileOutputStream;
import java.io.IOException;

public class PngReport {
    private final float Height = 54.0f;
    private final float Gain = Height / 840;
    private final float Scale = 0.1875f;
    private final float VScale = 1.35f;
    private final int labelPadding = 70;
    private int wavePadding = 430;
    private Canvas mCanvas;
    private Bitmap DisplayBitmap;
    private Paint mPaint;
    private short[] wbuffer;

    private boolean isSuccess = false;

    public PngReport(short[] data) {
        mPaint = new Paint();
        mPaint.setTextSize(30);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mPaint.setStrokeWidth(2.1f);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        wbuffer = data;
    }

    public boolean getPngState() {
        return isSuccess;
    }

    private String label[] = {"Ⅰ", "Ⅱ", "Ⅲ", "aVR", "aVL", "aVF", "V1", "V2",
            "V3", "V4", "V5", "V6"};

    public int drawEcg3x4(String path) {
        if (wbuffer == null) {
            return -1;
        }
        DisplayBitmap = Bitmap.createBitmap(2138, 375, Config.ARGB_8888);
        mCanvas = new Canvas(DisplayBitmap);
//		mCanvas.drawColor(Color.TRANSPARENT);
        mCanvas.drawColor(Color.WHITE);
        drawBackground(mCanvas, 2138, 375);
        int PRINT_H = 384;
        wavePadding = wavePadding + 95;
        // 波形绘制参数定义
        float[] lastGain = new float[12];
        float[] lastPos = new float[12];
        float[] labelPos = new float[12];
        float[] gain = new float[12];
        float[] pos = new float[12];
        float[] baseLine = new float[12];
        float[] flagBaseLine = new float[4];
        // 基线等参数初始化
        for (int i = 0; i < 3; i += 1) {
            lastGain[i] = baseLine[i] = (2 * i + 1) * PRINT_H * 1.0f / (2 * 3);
            lastGain[i + 3] = baseLine[i + 3] = (2 * i + 1) * PRINT_H * 1.0f
                    / (2 * 3);
            lastGain[i + 6] = baseLine[i + 6] = (2 * i + 1) * PRINT_H * 1.0f
                    / (2 * 3);
            lastGain[i + 9] = baseLine[i + 9] = (2 * i + 1) * PRINT_H * 1.0f
                    / (2 * 3);
        }
        for (int i = 0; i < 12; i++) {
            lastPos[i] = (i / 3) * wavePadding + labelPadding;
            labelPos[i] = (i / 3) * wavePadding + 20;
        }
        // 导联标号
        for (int i = 0; i < 12; i++) {
            mCanvas.drawText(label[i], labelPos[i], baseLine[i] + 10, mPaint);
        }
        // 增益及走速
        mCanvas.drawText("25mm/s   10mm/mV", labelPos[2] + 100,
                baseLine[2] + 40, mPaint);
        // 定标信号
        for (int i = 0; i < 4; i++) {
            flagBaseLine[i] = baseLine[3 * i + 1] - 30;
            mCanvas.drawLine(lastPos[3 * i + 1], flagBaseLine[i],
                    lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i], mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i],
                    lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i] - Height
                            * VScale, mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i]
                            - Height * VScale, lastPos[3 * i + 1] + 150 * Scale,
                    flagBaseLine[i] - Height * VScale, mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 150 * Scale, flagBaseLine[i]
                            - Height * VScale, lastPos[3 * i + 1] + 150 * Scale,
                    flagBaseLine[i], mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 150 * Scale, flagBaseLine[i],
                    lastPos[3 * i + 1] + 200 * Scale, flagBaseLine[i], mPaint);
        }
        // 12导波形绘制
        int len = 2500;
        if ((wbuffer.length / 12) < 2500) {
            len = wbuffer.length / 12;
        }
        for (int i = 0; i < len; i += 1) {
            for (int j = 0; j < 12; j++) {
                gain[j] = baseLine[j] - (wbuffer[j + i * 12]) * Gain;
                pos[j] = i * Scale + (j / 3) * wavePadding + labelPadding;
                if (i > 1) {
                    mCanvas.drawLine(lastPos[j], lastGain[j], pos[j], gain[j],
                            mPaint);
                }
                lastGain[j] = gain[j];
                lastPos[j] = pos[j];
            }
        }
        // 保存并输出png文件
//        mCanvas.save(Canvas.ALL_SAVE_FLAG);
        mCanvas.save();
        try {
            FileOutputStream fos = new FileOutputStream(path);
            DisplayBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            return -2;
        }
        return 0;
    }

    public int drawEcg6x2(String path) {
        if (wbuffer == null) {
            return -1;
        }
        DisplayBitmap = Bitmap.createBitmap(2025, 788, Config.ARGB_8888);
        mCanvas = new Canvas(DisplayBitmap);
//		mCanvas.drawColor(Color.TRANSPARENT);
        mCanvas.drawColor(Color.WHITE);
        drawBackground(mCanvas, 2025, 788);
        int PRINT_H = 768;
        wavePadding = wavePadding * 2 + 150;
        // 波形绘制参数定义
        float[] lastGain = new float[12];
        float[] lastPos = new float[12];
        float[] labelPos = new float[12];
        float[] gain = new float[12];
        float[] pos = new float[12];
        float[] baseLine = new float[12];
        float[] flagBaseLine = new float[4];
        // 基线等参数初始化
        for (int i = 0; i < 6; i += 1) {
            lastGain[i] = baseLine[i] = (2 * i + 1) * PRINT_H * 1.0f / (2 * 6);
            lastGain[i + 6] = baseLine[i + 6] = (2 * i + 1) * PRINT_H * 1.0f
                    / (2 * 6);
        }
        for (int i = 0; i < 12; i++) {
            lastPos[i] = (i / 6) * wavePadding + labelPadding;
            labelPos[i] = (i / 6) * wavePadding + 10;
        }

        // 导联标号
        for (int i = 0; i < 12; i++) {
            mCanvas.drawText(label[i], labelPos[i], baseLine[i] + 10, mPaint);
        }
        // 增益及走速
        mCanvas.drawText("25mm/s   10mm/mV", labelPos[5] + 100,
                baseLine[5] + 70, mPaint);
        // 定标信号
        for (int i = 0; i < 4; i++) {
            flagBaseLine[i] = baseLine[3 * i + 1] - 30;
            mCanvas.drawLine(lastPos[3 * i + 1], flagBaseLine[i],
                    lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i], mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i],
                    lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i] - Height
                            * VScale, mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i]
                            - Height * VScale, lastPos[3 * i + 1] + 150 * Scale,
                    flagBaseLine[i] - Height * VScale, mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 150 * Scale, flagBaseLine[i]
                            - Height * VScale, lastPos[3 * i + 1] + 150 * Scale,
                    flagBaseLine[i], mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 150 * Scale, flagBaseLine[i],
                    lastPos[3 * i + 1] + 200 * Scale, flagBaseLine[i], mPaint);
        }
        // 12导波形绘制
        int len = 5000;
        if ((wbuffer.length / 12) < 5000) {
            len = wbuffer.length / 12;
        }
        for (int i = 0; i < len; i += 1) {
            for (int j = 0; j < 12; j++) {
                gain[j] = baseLine[j] - (wbuffer[j + i * 12]) * Gain;
                pos[j] = i * Scale + (j / 6) * wavePadding + labelPadding;
                if (i > 1) {
                    mCanvas.drawLine(lastPos[j], lastGain[j], pos[j], gain[j],
                            mPaint);
                }
                lastGain[j] = gain[j];
                lastPos[j] = pos[j];
            }
        }
        // 保存并输出png文件
//        mCanvas.save(Canvas.ALL_SAVE_FLAG);
        mCanvas.save();
        try {
            FileOutputStream fos = new FileOutputStream(path);
            DisplayBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            return -2;
        }
        return 0;
    }

    public Bitmap getBitmap(String path,String gainShow,String speedShow) {
        if (wbuffer == null) {
            return null;
        }
        DisplayBitmap = Bitmap.createBitmap(2025, 788, Config.ARGB_8888);
        mCanvas = new Canvas(DisplayBitmap);
//		mCanvas.drawColor(Color.TRANSPARENT);
        mCanvas.drawColor(Color.WHITE);
        drawBackground(mCanvas, 2025, 788);
        int PRINT_H = 768;
        wavePadding = wavePadding * 2 + 150;
        // 波形绘制参数定义
        float[] lastGain = new float[12];
        float[] lastPos = new float[12];
        float[] labelPos = new float[12];
        float[] gain = new float[12];
        float[] pos = new float[12];
        float[] baseLine = new float[12];
        float[] flagBaseLine = new float[4];
        // 基线等参数初始化
        for (int i = 0; i < 6; i += 1) {
            lastGain[i] = baseLine[i] = (2 * i + 1) * PRINT_H * 1.0f / (2 * 6);

            lastGain[i + 6] = baseLine[i + 6] = (2 * i + 1) * PRINT_H * 1.0f
                    / (2 * 6);

        }
        for (int i = 0; i < 12; i++) {
            lastPos[i] = (i / 6) * wavePadding + labelPadding;
            labelPos[i] = (i / 6) * wavePadding + 10;
        }

        // 导联标号
        for (int i = 0; i < 12; i++) {
            mCanvas.drawText(label[i], labelPos[i], baseLine[i] + 10, mPaint);
        }
        // 增益及走速  动态设置
        mCanvas.drawText(speedShow+"mm/s    "+gainShow+"mm/mV", labelPos[5] + 100,
       // mCanvas.drawText("25mm/s   10mm/mV", labelPos[5] + 100,
                baseLine[5] + 70, mPaint);
        // 定标信号
        for (int i = 0; i < 4; i++) {
            flagBaseLine[i] = baseLine[3 * i + 1] - 30;
            mCanvas.drawLine(lastPos[3 * i + 1], flagBaseLine[i],
                    lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i], mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i],
                    lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i] - Height
                            * VScale, mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i]
                            - Height * VScale, lastPos[3 * i + 1] + 150 * Scale,
                    flagBaseLine[i] - Height * VScale, mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 150 * Scale, flagBaseLine[i]
                            - Height * VScale, lastPos[3 * i + 1] + 150 * Scale,
                    flagBaseLine[i], mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 150 * Scale, flagBaseLine[i],
                    lastPos[3 * i + 1] + 200 * Scale, flagBaseLine[i], mPaint);
        }
        // 12导波形绘制
        int len = 5000;
        if ((wbuffer.length / 12) < 5000) {
            len = wbuffer.length / 12;
        }
        for (int i = 0; i < len; i += 1) {
            for (int j = 0; j < 12; j++) {
                gain[j] = baseLine[j] - (wbuffer[j + i * 12]) * Gain;
                pos[j] = i * Scale + (j / 6) * wavePadding + labelPadding;
                if (i > 1) {
                    mCanvas.drawLine(lastPos[j], lastGain[j], pos[j], gain[j],
                            mPaint);
                }
                lastGain[j] = gain[j];
                lastPos[j] = pos[j];
            }
        }
        // 保存并输出png文件
//        mCanvas.save(Canvas.ALL_SAVE_FLAG);
        mCanvas.save();
        try {
            FileOutputStream fos = new FileOutputStream(path);
            DisplayBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
            isSuccess = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return DisplayBitmap;
    }

    public int drawEcg12x1(String path) {
        if (wbuffer == null) {
            return -1;
        }
        // 52*7.5*5
        // 42*7.5*5
        DisplayBitmap = Bitmap.createBitmap(1950, 1575, Config.ARGB_8888);
        mCanvas = new Canvas(DisplayBitmap);
//		mCanvas.drawColor(Color.TRANSPARENT);
        mCanvas.drawColor(Color.WHITE);
        drawBackground(mCanvas, 1950, 1575);
        int PRINT_H = 1572;
        // 波形绘制参数定义
        float[] lastGain = new float[12];
        float[] lastPos = new float[12];
        float[] labelPos = new float[12];
        float[] gain = new float[12];
        float[] pos = new float[12];
        float[] baseLine = new float[12];
        float[] flagBaseLine = new float[4];
        // 基线等参数初始化
        for (int i = 0; i < 12; i += 1) {
            lastGain[i] = baseLine[i] = (2 * i + 1) * PRINT_H * 1.0f / (2 * 12);

        }
        for (int i = 0; i < 12; i++) {
            lastPos[i] = labelPadding;
            labelPos[i] = 10;
        }

        // 导联标号
        for (int i = 0; i < 12; i++) {
            mCanvas.drawText(label[i], labelPos[i], baseLine[i] + 10, mPaint);
        }
        // 增益及走速
        mCanvas.drawText("25mm/s   10mm/mV", labelPos[11] + 100,
                baseLine[11] + 60, mPaint);

        // 定标信号
        for (int i = 0; i < 4; i++) {
            flagBaseLine[i] = baseLine[3 * i + 1] - 30;
            mCanvas.drawLine(lastPos[3 * i + 1], flagBaseLine[i],
                    lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i], mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i],
                    lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i] - Height
                            * VScale, mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 50 * Scale, flagBaseLine[i]
                            - Height * VScale, lastPos[3 * i + 1] + 150 * Scale,
                    flagBaseLine[i] - Height * VScale, mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 150 * Scale, flagBaseLine[i]
                            - Height * VScale, lastPos[3 * i + 1] + 150 * Scale,
                    flagBaseLine[i], mPaint);
            mCanvas.drawLine(lastPos[3 * i + 1] + 150 * Scale, flagBaseLine[i],
                    lastPos[3 * i + 1] + 200 * Scale, flagBaseLine[i], mPaint);
        }
        // 12导波形绘制
        int len = 10000;
        if ((wbuffer.length / 12) < 10000) {
            len = wbuffer.length / 12;
        }
        for (int i = 0; i < len; i += 1) {
            for (int j = 0; j < 12; j++) {
                gain[j] = baseLine[j] - (wbuffer[j + i * 12]) * Gain;
                pos[j] = i * Scale + labelPadding;
                if (i > 1) {
                    mCanvas.drawLine(lastPos[j], lastGain[j], pos[j], gain[j],
                            mPaint);
                }
                lastGain[j] = gain[j];
                lastPos[j] = pos[j];
            }
        }
        // 保存并输出png文件
//        mCanvas.save(Canvas.ALL_SAVE_FLAG);
        mCanvas.save();
        try {
            FileOutputStream fos = new FileOutputStream(path);
            DisplayBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            return -2;
        }
        return 0;
    }

    private void drawBackground(Canvas canvas, int width, int height) {
        Paint bPaint = new Paint();

//		bPaint.setColor(Color.GRAY);
        bPaint.setColor(Color.rgb(240, 143, 143));
        bPaint.setStrokeWidth(1.0f);
        for (int i = 0; i < (height / (Scale * 40)); i++) {
            float y = i * (Scale * 40);
            if (i % 5 == 0) {
                // 画横线
                canvas.drawLine(0, y, width, y, bPaint);
            }
            for (int j = 0; j < (width / (Scale * 40)); j++) {
                float x = j * (Scale * 40);
                if (j % 5 == 0 && i == 0) {
                    // 画竖线
                    canvas.drawLine(x, y, x, height, bPaint);
                } else if (j % 5 != 0 && i % 5 != 0) {
                    canvas.drawPoint(x, y, bPaint);
                }
            }
        }

        bPaint.setColor(Color.BLACK);
        bPaint.setStrokeWidth(2.0f);
        canvas.drawLine(1, 1, width - 1, 1, bPaint);
        canvas.drawLine(1, 1, 1, height - 1, bPaint);
        canvas.drawLine(1, height - 1, width - 1, height - 1, bPaint);
        canvas.drawLine(width - 1, 1, width - 1, height - 1, bPaint);
    }
}
